/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2010 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedm@list.ru
 */



#ifndef NTSERVICE_H
#define NTSERVICE_H
#include "windows.h"
#include "def.h"
#include <QString>


extern int tbn_main(int ,char **);

DWORD dwErrCode; // error code
SERVICE_STATUS ss; //status of service
SERVICE_STATUS_HANDLE ssHandle; // descriptor of service
TCHAR sm[]=TEXT(d_appname);
LPTSTR SomeServiceName = sm;

//install service
bool instserv(const QString & paths)
{
    SC_HANDLE hService; //
    SC_HANDLE hSCManager;
        // install service to system
            //open Service Control Manager
            hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
            if(!hSCManager) {
                qDebug()<<"Can't open SCM.";
                return false;
            }
            // create SomeService
            hService = CreateServiceA(hSCManager,d_appname,//SomeServiceName,
            d_appname,//SomeServiceName,
            SERVICE_ALL_ACCESS,
            SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,
            SERVICE_ERROR_NORMAL,
            paths.toLocal8Bit(),//path to exe service
            NULL, NULL, NULL, NULL, NULL);
            if(!hService) {
                qDebug()<<"Can't create service";
                CloseServiceHandle(hSCManager);
                return false;
            }
            CloseServiceHandle(hService);
            CloseServiceHandle(hSCManager);
            qDebug()<<"Create service success";
            return true;


}

bool delserv(){
    SC_HANDLE hService;
    SC_HANDLE hSCManager;
    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if(!hSCManager) {
               qDebug()<<"error open SCM";
               return false;
               }
    hService = OpenService(hSCManager, SomeServiceName,
    SERVICE_STOP | DELETE);
    if(!hService) {
        qDebug()<<"error open service";
        return false;
    }
    if(ss.dwCurrentState != SERVICE_STOPPED)
               ControlService(hService, SERVICE_CONTROL_STOP, &ss);
               DeleteService(hService);
               CloseServiceHandle(hService);
               CloseServiceHandle(hSCManager);
               return true;
    }


bool StopSomeService() {
//...
return TRUE;
}

void SetSomeServiceStatus(DWORD dwCurrentState,
      DWORD dwWin32ExitCode,
      DWORD dwWaitHint){
  static DWORD dwCheckPoint = 1;
  if(dwCurrentState == SERVICE_START_PENDING){
    ss.dwControlsAccepted = 0;
  }else{
    ss.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  }
    ss.dwCurrentState = dwCurrentState;
    ss.dwWin32ExitCode = dwWin32ExitCode;
    ss.dwWaitHint = dwWaitHint;
  if(dwCurrentState == SERVICE_RUNNING || dwCurrentState == SERVICE_STOPPED){
    ss.dwCheckPoint = 0;
  }else{
    ss.dwCheckPoint = dwCheckPoint++;
  }
SetServiceStatus(ssHandle, &ss);
}


void WINAPI ServiceControl(DWORD dwControlCode) {
  switch(dwControlCode) {
    case SERVICE_CONTROL_STOP: {
      SetSomeServiceStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
      StopSomeService();
      SetSomeServiceStatus(SERVICE_STOPPED, NO_ERROR, 0);
      break;
    }
    case SERVICE_CONTROL_INTERROGATE: {
      SetSomeServiceStatus(ss.dwCurrentState, NO_ERROR, 0);
      break;
    }
    default: {
      SetSomeServiceStatus(ss.dwCurrentState, NO_ERROR, 0);
      break;
    }
  }
}




int WINAPI ServiceMain(DWORD argc, LPSTR* argv) {
  ssHandle = RegisterServiceCtrlHandler(SomeServiceName, ServiceControl);
  if(!ssHandle) {
    printf("Error registering ServiceControl\n");
    return 3;
  }
  ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  SetSomeServiceStatus(SERVICE_START_PENDING, NO_ERROR, 4000);
  //InitSomeServiceData(argc, argv);
  SetSomeServiceStatus(SERVICE_RUNNING, NO_ERROR, 0);

  return tbn_main(argc,argv);
  //core.start();
  //return a.exec();
}


#endif // NTSERVICE_H
